package com.xbai.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbai.goods.dao.BrandMapper;
import com.xbai.goods.dao.CategoryMapper;
import com.xbai.goods.pojo.Brand;
import com.xbai.goods.pojo.Category;
import com.xbai.goods.service.BrandService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @desc Brand业务层实现类
 * @author jxbai
 * @date 2021/1/28
 */
@Service
@RequiredArgsConstructor
public class BrandServiceImpl implements BrandService {

    private final BrandMapper brandMapper;


    /**
     * 查询分类对应的品牌集合
     * @param categoryId 分类id
     * @return {@link Brand}
     */
    @Override
    public List<Brand> findByCategoryId(Integer categoryId) {
        return brandMapper.findByCategoryId(categoryId);
    }

    /**
     * Brand条件+分页查询
     * @param brand {@link Brand}
     * @param page 页码
     * @param size 页大小
     * @return {@link PageInfo< Brand >} 分页结果
     */
    @Override
    public PageInfo<Brand> findPage(Brand brand, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(brand);
        //执行搜索
        return new PageInfo<>(brandMapper.selectByExample(example));
    }

    /**
     * Brand分页查询
     * @param page 页码
     * @param size 页大小
     * @return {@link PageInfo< Brand >}
     */
    @Override
    public PageInfo<Brand> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<>(brandMapper.selectAll());
    }

    /**
     * Brand条件查询
     * @param brand {@link Brand}
     * @return {@link List < Brand >}
     */
    @Override
    public List<Brand> findList(Brand brand){
        //构建查询条件
        Example example = createExample(brand);
        //根据构建的条件查询数据
        return brandMapper.selectByExample(example);
    }


    /**
     * Brand构建查询对象
     * @param brand {@link Brand}
     * @return {@link Example} 条件对象
     */
    private Example createExample(Brand brand){
        Example example=new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(brand!=null){
            // 品牌id
            if(!StringUtils.isEmpty(brand.getId())){
                    criteria.andEqualTo("id",brand.getId());
            }
            // 品牌名称
            if(!StringUtils.isEmpty(brand.getName())){
                    criteria.andLike("name","%"+brand.getName()+"%");
            }
            // 品牌图片地址
            if(!StringUtils.isEmpty(brand.getImage())){
                    criteria.andEqualTo("image",brand.getImage());
            }
            // 品牌的首字母
            if(!StringUtils.isEmpty(brand.getLetter())){
                    criteria.andEqualTo("letter",brand.getLetter());
            }
            // 排序
            if(!StringUtils.isEmpty(brand.getSeq())){
                    criteria.andEqualTo("seq",brand.getSeq());
            }
        }
        return example;
    }

    /**
     * 删除Brand
     * @param id 主键id
     */
    @Override
    public void delete(Integer id){
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Brand数据
     * @param brand {@link Brand}
     */
    @Override
    public void update(Brand brand){
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 新增Brand
     * @param brand {@link Brand}
     */
    @Override
    public void add(Brand brand){
        brandMapper.insert(brand);
    }

    /**
     * 根据ID查询Brand
     * @param id 主键id
     * @return {@link Brand}
     */
    @Override
    public Brand findById(Integer id){
        return  brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有Brand
     * @return {@link List < Brand >}
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }
}
